﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using PB.Airsoft.Core;
using PB.Airsoft.Business.Account;
using PB.Airsoft.Business.Account.Entities;
using PB.Airsoft.Business.Account.Interfaces;
using PB.Airsoft.Business.User;

namespace PB.Airsoft.Web.Business.Membership
{
    public class PBMembershipProvider : MembershipProvider
    {
        #region Private members

        protected string applicationName = "/";
        protected bool enablePasswordReset = false;
        protected bool enablePasswordRetrieval = false;
        protected int maxInvalidPasswordAttempts = 3;
        protected int minRequiredNonAlphanumericCharacters = 0;
        protected int minRequiredPasswordLength = 7;
        protected MembershipPasswordFormat passwordFormat = MembershipPasswordFormat.Clear;
        protected string passwordStrengthRegularExpression = string.Empty;
        protected bool requiresQuestionAndAnswer = false;
        protected bool requiresUniqueEmail = true;

        #endregion

        #region Public properties

        public override string ApplicationName
        {
            get { return applicationName; }
            set { applicationName = value; }
        }

        public override bool EnablePasswordReset
        {
            get { return enablePasswordReset; }
        }

        public override bool EnablePasswordRetrieval
        {
            get { return enablePasswordRetrieval; }
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { return maxInvalidPasswordAttempts; }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return minRequiredNonAlphanumericCharacters; }
        }

        public override int MinRequiredPasswordLength
        {
            get { return minRequiredPasswordLength; }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { return passwordFormat; }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { return passwordStrengthRegularExpression; }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { return requiresQuestionAndAnswer; }
        }

        public override bool RequiresUniqueEmail
        {
            get { return requiresUniqueEmail; }
        }

        #endregion

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotImplementedException("ChangePassword");
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException("ChangePasswordQuestionAndAnswer");
        }

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            var user = UserManager.CreateUser(username);

            var userAccount = new UserAccount()
            {
                Id = Guid.NewGuid(),
                UserId = user.Id,
                Email = email,
                Password = password
            };
            AccountManager.CreateUserAccount(userAccount);

            status = MembershipCreateStatus.Success;
            return Parse(userAccount);
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException("DeleteUser");
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException("FindUsersByEmail");
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException("FindUsersByName");
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException("GetAllUsers");
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException("GetNumberOfUsersOnline");
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException("GetPassword");
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            return Parse(AccountManager.GetUserAccountByEmail(username));
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException("GetUser");
        }

        public override string GetUserNameByEmail(string email)
        {
            return email;
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException("ResetPassword");
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException("UnlockUser");
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException("UpdateUser");
        }

        public override bool ValidateUser(string username, string password)
        {
            var userAccount = AccountManager.GetUserAccountByEmail(username);
            if (userAccount == null)
                return false;

            return (userAccount.Password == password);
        }

        private MembershipUser Parse(IUserAccount userAccount)
        {
            var membershipUser =
                new MembershipUser(this.Name, userAccount.Email, userAccount.Id, userAccount.Email,
                    null, null, true, false,
                    DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);

            return membershipUser;
        }
    }
}
